# Copyright (C) ARM Limited, 2007-2016. All rights reserved.
#
# This example is intended to be built with ARM Compiler 6 with SVE
#
# Environment variables for build options that the user might wish to change
#
# Variable     Example Value
# ----------   -------------
# APP          myapp.axf
# QUIET        @ for terse output, or leave blank for detailed output
# OPT_LEVEL    0, 1, 2 or 3
# DEFINES      -D MYDEFINE
# SUPPRESS     --diag_suppress=1234

APP ?= sve_array_sum.axf
OPT_LEVEL ?= 1
LIBS ?=

# Other switches the user should not normally need to change:
ARCH = armv8-a+sve
DEBUG_FLAGS = -g

CC = armclang

define EOL =


endef

ifeq ($(OS),Windows_NT)
RM_FILES = $(foreach file,$(1),if exist $(file) del /q $(file)$(EOL))
RM_DIRS = $(foreach dir,$(1),if exist $(dir) rmdir /s /q $(dir)$(EOL))
else
RM_FILES = $(foreach file,$(1),rm -f $(file)$(EOL))
RM_DIRS = $(foreach dir,$(1),rm -rf $(dir)$(EOL))
endif

DEPEND_FLAGS = -MD

CPPFLAGS = $(DEFINES) $(INCLUDES) $(DEPEND_FLAGS)
CFLAGS = $(DEBUG_FLAGS) -O$(OPT_LEVEL) -fvectorize
ASFLAGS = $(DEBUG_FLAGS) $(SUPPRESS)
LDFLAGS = -Wl,--ro-base=0x80000000
TARGET_ARCH = --target=aarch64-arm-none-eabi -march=$(ARCH) # Used for .c
TARGET_MACH = $(TARGET_ARCH) # Used for .S

APP_C_SRC := $(wildcard *.c)
APP_ASM_SRC := $(wildcard *.S)
OBJ_FILES := $(APP_C_SRC:%.c=%.o) $(APP_ASM_SRC:%.S=%.o)
DEP_FILES := $(OBJ_FILES:%.o=%.d)

.phony: all clean

all: $(APP)

$(APP): $(OBJ_FILES)
	$(CC) $(TARGET_ARCH) $(LDFLAGS) -o $@ $(OBJ_FILES) $(LIBS)

clean:
	$(call RM_FILES,$(APP) $(OBJ_FILES) $(DEP_FILES))

# Make sure everything is rebuilt if this makefile is changed
$(OBJ_FILES) $(APP): Makefile

-include $(DEP_FILES)
